package com.tangyh.basic.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tangyh.basic.alipay.bean.SendSmsBean;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SendSms {
    //请求路径
    private static final String url = "dysmsapi.aliyuncs.com";
    //版本号
    private static final String version = "2017-05-25";
    //请求动作
    private static final String action = "SendSms";

    /**
     * 发送用户签约短信
     *
     * @param sendSmsBean
     * @throws ClientException
     */
    public static CommonResponse sendSms(SendSmsBean sendSmsBean) {
        String accessKeyId = sendSmsBean.getAccessKeyId();
        String accessKeySecret = sendSmsBean.getAccessKeySecret();
        String signName = sendSmsBean.getMessageSignName();
        String template = sendSmsBean.getTemplateCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(url);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendSmsBean.getMobile());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"name\":\"" + sendSmsBean.getName() + "\"}");
        log.info("发送参数:name" + sendSmsBean.getName() + ",template:" + template + "messageSignName:" + signName);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    //测试
    public static void main(String[] args) {
        SendSmsBean sendSmsBean = new SendSmsBean();
        sendSmsBean.setAccessKeyId("LTAI4FsB9pB9AkiqDR8CgvUi");
        sendSmsBean.setAccessKeySecret("5jYyGd9yfxiNbGdTDLlqo4eYu65gPA");
        sendSmsBean.setCode("123456");
        sendSmsBean.setMobile("15959008715");
        sendSmsBean.setTemplateCode("SMS_222336546");
        sendSmsBean.setMessageSignName("企进");
        sendSignSms(sendSmsBean, "涨", "1");
    }

    /**
     * 发送签约验证码短信
     *
     * @param sendSmsBean
     * @return
     * @throws ClientException
     */
    public static CommonResponse sendSignSms(SendSmsBean sendSmsBean, String name, String merchantId) {
        String accessKeyId = sendSmsBean.getAccessKeyId();
        String accessKeySecret = sendSmsBean.getAccessKeySecret();
        String signName = sendSmsBean.getMessageSignName();
        String template = sendSmsBean.getTemplateCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(url);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendSmsBean.getMobile());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"name\":\"" + name + "\",,\"merchantId\":\"" + merchantId + "\"}");
        log.info("发送参数:name" + name + ",template:" + template + "messageSignName:" + signName);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送验证码短信
     *
     * @param sendSmsBean
     * @return
     * @throws ClientException
     */
    public static CommonResponse sendCodeSms(SendSmsBean sendSmsBean) {
        String accessKeyId = sendSmsBean.getAccessKeyId();
        String accessKeySecret = sendSmsBean.getAccessKeySecret();
        String signName = sendSmsBean.getMessageSignName();
        String template = sendSmsBean.getTemplateCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(url);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendSmsBean.getMobile());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + sendSmsBean.getCode() + "\"}");
        log.info("发送参数:code" + sendSmsBean.getCode() + ",template:" + template + "messageSignName:" + signName);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送商户账号密码支付密码短信
     *
     * @param sendSmsBean
     * @return
     * @throws ClientException
     */
    public static CommonResponse sendUserSms(SendSmsBean sendSmsBean) {
        String accessKeyId = sendSmsBean.getAccessKeyId();
        String accessKeySecret = sendSmsBean.getAccessKeySecret();
        String signName = sendSmsBean.getMessageSignName();
        String template = sendSmsBean.getTemplateCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(url);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendSmsBean.getMobile());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"username\":\"" + sendSmsBean.getUsername() + "\",\"password\":\"" + sendSmsBean.getPassword() + "\",\"payPassword\":\"" + sendSmsBean.getPayPassword() + "\"}");
        log.info("发送参数:账号" + sendSmsBean.getUsername() + "，密码" + sendSmsBean.getPassword() + ",支付密码" + sendSmsBean.getPayPassword() + ",template:" + template + "messageSignName:" + signName);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送子账号登录账号密码
     *
     * @param sendSmsBean
     * @return
     * @throws ClientException
     */
    public static CommonResponse sendCreateUserSms(SendSmsBean sendSmsBean) {
        String accessKeyId = sendSmsBean.getAccessKeyId();
        String accessKeySecret = sendSmsBean.getAccessKeySecret();
        String signName = sendSmsBean.getMessageSignName();
        String template = sendSmsBean.getTemplateCode();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(url);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sendSmsBean.getMobile());
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"username\":\"" + sendSmsBean.getUsername() + "\",\"password\":\"" + sendSmsBean.getPassword() + "\"}");
        log.info("发送参数:账号" + sendSmsBean.getUsername() + "，密码" + sendSmsBean.getPassword() + ",phoneNum:" + sendSmsBean.getMobile() + ",template:" + template + "messageSignName:" + signName);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

}
